package com.craftworks.tiles.impl;

import com.craftworks.tiles.AbstractTile;

import java.awt.*;
import java.util.List;

public class RingTile extends AbstractTile
{
    private double ring1Value;
    private double ring2Value;
    private double ring3Value;
    private Color ring1Color;
    private Color ring2Color;
    private Color ring3Color;

    private static final Color COLOR_1 = Color.decode("#ff2b59");
    private static final Color COLOR_2 = Color.decode("#ffa600");
    private static final Color COLOR_3 = Color.decode("#009be8");

    public RingTile()
    {
        this(1.0, 1.0, 1.0);
    }

    public RingTile(double ring1, double ring2, double ring3)
    {
        super(Size.LANDSCAPE);
        ring1Value = Math.max(0, Math.min(1, ring1));
        ring2Value = Math.max(0, Math.min(1, ring2));
        ring3Value = Math.max(0, Math.min(1, ring3));
        ring1Color = COLOR_1;
        ring2Color = COLOR_2;
        ring3Color = COLOR_3;
    }

    @Override
    public List<String> propertyKeys()
    {
        return List.of("ring1.value", "ring1.color",
                       "ring2.value", "ring2.color",
                       "ring3.value", "ring3.color");
    }

    @Override
    public void setProperty(String key, Object value)
    {
        switch (key)
        {
            case "ring1.value" ->
            {
                switch (value)
                {
                    case null -> ring1Value = 0;
                    case Number n -> ring1Value = Math.max(0, Math.min(1, n.doubleValue()));
                    case String s -> ring1Value = Math.max(0, Math.min(1, Double.parseDouble(s)));
                    default -> throw new IllegalStateException("Unexpected value: " + value);
                }
            }
            case "ring1.color" ->
            {
                switch (value)
                {
                    case null -> ring1Color = COLOR_1;
                    case Color c -> ring1Color = c;
                    case String s -> ring1Color = Color.decode(s);
                    default -> throw new IllegalStateException("Unexpected value: " + value);
                }
            }
            case "ring2.value" ->
            {
                switch (value)
                {
                    case null -> ring2Value = 0;
                    case Number n -> ring2Value = Math.max(0, Math.min(1, n.doubleValue()));
                    case String s -> ring2Value = Math.max(0, Math.min(1, Double.parseDouble(s)));
                    default -> throw new IllegalStateException("Unexpected value: " + value);
                }
            }
            case "ring2.color" ->
            {
                switch (value)
                {
                    case null -> ring2Color = COLOR_2;
                    case Color c -> ring2Color = c;
                    case String s -> ring2Color = Color.decode(s);
                    default -> throw new IllegalStateException("Unexpected value: " + value);
                }
            }
            case "ring3.value" ->
            {
                switch (value)
                {
                    case null -> ring3Value = 0;
                    case Number n -> ring3Value = Math.max(0, Math.min(1, n.doubleValue()));
                    case String s -> ring3Value = Math.max(0, Math.min(1, Double.parseDouble(s)));
                    default -> throw new IllegalStateException("Unexpected value: " + value);
                }
            }
            case "ring3.color" ->
            {
                switch (value)
                {
                    case null -> ring3Color = COLOR_3;
                    case Color c -> ring3Color = c;
                    case String s -> ring3Color = Color.decode(s);
                    default -> throw new IllegalStateException("Unexpected value: " + value);
                }
            }
            default ->
            {
                return;
            }
        }
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        Stroke stroke = new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
        g2d.setStroke(stroke);

        int cx = size.width / 2 - 25;
        int cy = size.height / 2;

        g2d.setPaint(ring1Color);
        g2d.drawArc(cx - 36, cy - 36, 72, 72, 90, (int) (ring1Value * 360));
        g2d.setPaint(ring2Color);
        g2d.drawArc(cx - 24, cy - 24, 48, 48, 90, (int) (ring2Value * 360));
        g2d.setPaint(ring3Color);
        g2d.drawArc(cx - 12, cy - 12, 24, 24, 90, (int) (ring3Value * 360));

        g2d.setPaint(ring1Color);
        g2d.fillOval(size.width - 45, 12, 10, 10);
        g2d.drawString(String.format("%.1f", ring1Value), size.width - 30, 21);
        g2d.setPaint(ring2Color);
        g2d.fillOval(size.width - 45, 28, 10, 10);
        g2d.drawString(String.format("%.1f", ring2Value), size.width - 30, 38);
        g2d.setPaint(ring3Color);
        g2d.fillOval(size.width - 45, 44, 10, 10);
        g2d.drawString(String.format("%.1f", ring3Value), size.width - 30, 54);
    }
}
